
package com.hlkj.pay.service.ad.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.framework.mybatis.core.query.QueryWrapperX;
import com.hlkj.framework.mybatis.core.utils.MyBatisUtils;
import com.hlkj.pay.dto.ad.AdRefundOrderDto;
import com.hlkj.pay.dto.ad.query.AdRefundOrderQueryDto;
import com.hlkj.pay.infrastructure.mapper.ad.AdOrderRefundApplyMapper;
import com.hlkj.pay.infrastructure.mapper.ad.AdOrderRefundMapper;
import com.hlkj.pay.infrastructure.model.ad.AdRefundOrderApplyDO;
import com.hlkj.pay.infrastructure.model.ad.AdRefundOrderDO;
import com.hlkj.pay.service.ad.IAdRefundOrderService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/23 13:04
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AdRefundOrderServiceImpl implements IAdRefundOrderService {

    private final AdOrderRefundMapper adOrderRefundMapper;

    private final AdOrderRefundApplyMapper adOrderRefundApplyMapper;

    @Override
    public PageResult<AdRefundOrderDto> queryPage(AdRefundOrderQueryDto adRefundOrderQueryDto) {
        adRefundOrderQueryDto.setSortingFields("refund.id");
        IPage<AdRefundOrderDto> mpPage = MyBatisUtils.buildPage(adRefundOrderQueryDto);
        QueryWrapper<AdRefundOrderDto> queryWrapper = buildWrapper(adRefundOrderQueryDto);
        adOrderRefundMapper.selectRefundPage(mpPage, queryWrapper);
        return new PageResult(mpPage.getRecords(), mpPage.getTotal(), mpPage.getCurrent(), mpPage.getSize(), mpPage.getPages());
    }
    @Override
    public PageResult<AdRefundOrderApplyDO> queryApplyPage(AdRefundOrderQueryDto adRefundOrderQueryDto) {
        return adOrderRefundApplyMapper.selectPage(adRefundOrderQueryDto,buildApplyWrapper(adRefundOrderQueryDto));
    }
    
    @Override
    public AdRefundOrderDO queryDetail(String refundOrderNo) {
        return adOrderRefundMapper.selectOne(AdRefundOrderDO::getRefundOrderNo, refundOrderNo);
    }

    @Override
    public void updateRefundOrder(String refundOrderNo, String refundApplyNo, AdRefundOrderDO updateRefundOrderDO) {
        adOrderRefundMapper.updateById(updateRefundOrderDO);
        AdRefundOrderApplyDO adRefundOrderApplyDO = new AdRefundOrderApplyDO();
        adRefundOrderApplyDO.setRefundStatus(updateRefundOrderDO.getRefundStatus());

        AdRefundOrderQueryDto adRefundOrderQueryDto = new AdRefundOrderQueryDto();
        adRefundOrderQueryDto.setRefundApplyNo(refundApplyNo);
        adOrderRefundApplyMapper.update(adRefundOrderApplyDO, buildApplyWrapper(adRefundOrderQueryDto));
    }

    @Override
    public void addRefundOrder(AdRefundOrderDO adRefundOrderDO) {
        adOrderRefundMapper.insert(adRefundOrderDO);
    }

    @Override
    public AdRefundOrderApplyDO queryLastOne(String mchOrderNo) {
        AdRefundOrderQueryDto adRefundOrderQueryDto = new AdRefundOrderQueryDto();
        adRefundOrderQueryDto.setMchOrderNo(mchOrderNo);
        LambdaQueryWrapper<AdRefundOrderApplyDO> lambdaQueryWrapper = buildApplyWrapper(adRefundOrderQueryDto);
        lambdaQueryWrapper.orderByDesc(AdRefundOrderApplyDO::getId);
        lambdaQueryWrapper.last("limit 1");
        return adOrderRefundApplyMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public AdRefundOrderApplyDO queryByRefundApplyNo(String refundApplyNo) {
        return adOrderRefundApplyMapper.selectOne(AdRefundOrderApplyDO::getRefundApplyNo, refundApplyNo);
    }

    @Override
    public void addRefundOrderApply(AdRefundOrderApplyDO adRefundOrderApplyDO) {
        adOrderRefundApplyMapper.insert(adRefundOrderApplyDO);
    }

    @Override
    public void updateRefundOrderApply(AdRefundOrderApplyDO adRefundOrderApplyDO) {
        adOrderRefundApplyMapper.updateById(adRefundOrderApplyDO);
    }

    /**
     * 查询条件封装
     *
     * @param adRefundOrderQueryDto
     * @return
     */
    LambdaQueryWrapper<AdRefundOrderApplyDO> buildApplyWrapper(AdRefundOrderQueryDto adRefundOrderQueryDto) {
        LambdaQueryWrapperX<AdRefundOrderApplyDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(AdRefundOrderApplyDO::getRefundApplyNo, adRefundOrderQueryDto.getRefundApplyNo());
        queryWrapper.eqIfPresent(AdRefundOrderApplyDO::getApplyStatus, adRefundOrderQueryDto.getApplyStatus());
        queryWrapper.eqIfPresent(AdRefundOrderApplyDO::getInsOrderNo, adRefundOrderQueryDto.getInsOrderNo());
        queryWrapper.likeIfPresent(AdRefundOrderApplyDO::getInsOrderNo, adRefundOrderQueryDto.getLikeInsOrderNo());
        queryWrapper.eqIfPresent(AdRefundOrderApplyDO::getMchOrderNo, adRefundOrderQueryDto.getMchOrderNo());
        queryWrapper.eqIfPresent(AdRefundOrderApplyDO::getPhone, adRefundOrderQueryDto.getPhone());
        queryWrapper.likeIfPresent(AdRefundOrderApplyDO::getPhone, adRefundOrderQueryDto.getLikePhone());
        queryWrapper.geIfPresent(AdRefundOrderApplyDO::getCreateTime, adRefundOrderQueryDto.getStartQueryTime());
        queryWrapper.leIfPresent(AdRefundOrderApplyDO::getCreateTime, adRefundOrderQueryDto.getEndQueryTime());
        adRefundOrderQueryDto.setSortingFields("id");
        return queryWrapper;
    }

    /**
     * 查询条件封装
     *
     * @param adRefundOrderQueryDto
     * @return
     */
    QueryWrapper<AdRefundOrderDto> buildWrapper(AdRefundOrderQueryDto adRefundOrderQueryDto) {
        QueryWrapperX<AdRefundOrderDto> queryWrapper = new QueryWrapperX<>();
        queryWrapper.likeIfPresent("refund.mch_order_no", adRefundOrderQueryDto.getMchOrderNo());
        queryWrapper.likeIfPresent("refund.refund_apply_no", adRefundOrderQueryDto.getRefundApplyNo());
        queryWrapper.likeIfPresent( "refund.refund_order_no", adRefundOrderQueryDto.getRefundOrderNo());
        queryWrapper.likeIfPresent("info.name", adRefundOrderQueryDto.getAdName());
        if (StringUtils.hasText(adRefundOrderQueryDto.getLikeRefundOrderNo())) {
            queryWrapper
                    .like("refund.mch_order_no", adRefundOrderQueryDto.getLikeRefundOrderNo())
                    .or().like("refund.refund_apply_no", adRefundOrderQueryDto.getLikeRefundOrderNo())
                    .or().like("refund.refund_order_no", adRefundOrderQueryDto.getLikeRefundOrderNo())
                    .or().like("refund.plat_refund_order_no", adRefundOrderQueryDto.getLikeRefundOrderNo());
        }
        
        queryWrapper.eqIfPresent( "refund.ad_id", adRefundOrderQueryDto.getAdId());
        queryWrapper.eqIfPresent( "refund.refund_status", adRefundOrderQueryDto.getRefundStatus());
        queryWrapper.eqIfPresent("info.media_id", adRefundOrderQueryDto.getMediaId());
        queryWrapper.geIfPresent( "refund.create_time", adRefundOrderQueryDto.getStartQueryTime());
        queryWrapper.leIfPresent("refund.create_time", adRefundOrderQueryDto.getEndQueryTime());
        return queryWrapper;
    }
}
